package com.cloudptt.api.product.adapter;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.models.ErrInfo;
import com.cloudptt.api.models.lbs.ReportData;
import com.cloudptt.api.product.IGetReportData;
import com.cloudptt.api.product.ISuperCallOperator;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.group.HalfCallInfo;
import com.cloudptt.api.product.func.call.single.CallInfo;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.res.Resource;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.UserInfo;

import android.content.Context;

import java.util.List;

public class PCallEventAdapter 
{
	private static String THIS_FILE = "PCallEventAdapter";
	private Context ctx = null;
	private ISuperCallOperator callOperator = null;
	private IGetReportData reportDate = null;
	public PCallEventAdapter(Context ctx,ISuperCallOperator callOperator, IGetReportData reportDate)
	{
		this.ctx = ctx;
		this.callOperator = callOperator;
		this.reportDate = reportDate;
	}
	
	public void makePrivateCall(AddrInfo info, int callType, boolean useVideo , boolean isEmergency, String funcNum)
	{
		Log.d(THIS_FILE,"makePrivateCall callType = " + callType + " useVideo = " + useVideo + " funcNum = " + funcNum);
//		if(useVideo)
//		{
//			info.updateOpNum(CallAttr.McType_McVideo);
//		}
//		else
//		{
//			info.updateOpNum(CallAttr.McType_McPtt);
//		}
		List<HalfCallInfo> groupCallInfos = callOperator.getGroupCallInfoList();
		if(groupCallInfos != null
				&& groupCallInfos.size() > 0)
		{
			String str = Resource.getString("has_call");//ctx.getString(R.string.null_group);
			CommDlg.showShortToast(ctx, str);
			return;
		}
		String locationStr = "";
		ReportData data = reportDate.getReportData();
		if(data != null)
		{
			locationStr = data.toString();
		}
		CallAttr callAttr = new CallAttr(callType,info,CallAttr.CallDir_MO,useVideo,isEmergency,funcNum,locationStr);
		Log.d(THIS_FILE,"makePrivateCall callAttr = " + callAttr);
		int reuslt = callOperator.newCall(callAttr);
		Log.d(THIS_FILE,"makePrivateCall reuslt = " + reuslt);
		int action = CallConst.BtnAction_Make;
		if(useVideo)
		{
			action = CallConst.BtnAction_Make_Video;
		}
		if(CauseCont.OperateCheck_Suc == reuslt)
		{
			String callKey = callAttr.getCallKey();
			reuslt = callOperator.doUserAction(callKey,action,"");
			if(reuslt != CauseCont.OperateCheck_Suc) 
			{
				String errStr = CauseCont.getOperateCheckErrStr(reuslt,ctx);
				Log.d(THIS_FILE, "makePrivateCall doUserAction errStr = " + errStr);
				CommDlg.showToast(ctx,errStr);
				return;
			}
		}
		else
		{
			ErrInfo errInfo = new ErrInfo(CauseCont.ERROR_TITLE_COMM, reuslt);
			if( errInfo != null )
			{
				String errStr = errInfo.getErrDesc();
				Log.d(THIS_FILE, "makePrivateCall errStr = " + errStr);
				CommDlg.showToast(ctx, errStr);
			}
		}
	}
	public void clearResource()
	{
		callOperator = null;
		ctx = null;
	}
}
